<script setup lang="ts">
import { Button as VanButton } from 'vant' // 局部组件
import { useUserStore } from './stores'
import { request } from './utils/request'

const store = useUserStore()

const login = async () => {
  // 模拟登录成功 存储数据
  // store.setUser({
  //   token: '1',
  //   id: '1',
  //   account: '1',
  //   avatar: '1',
  //   mobile: '1'
  // })

  // 测试一下登录请求
  // const res = await instance({
  //   method: 'post',
  //   url: '/login/password',
  //   data: {
  //     mobile: '13211112222',
  //     password: 'abc12345'
  //   }
  //   // params: {}
  // })

  const res = await request<{
    account: string
    avatar: string
    id: string
    mobile: string
    refreshToken: string
    token: string
  }>('/login/password', 'post', {
    mobile: '13211112222',
    password: 'abc12345'
  })

  // console.log(res.data)
  store.setUser(res.data) // 存仓库 + 存本地
}

const fn = async () => {
  // 测试request泛型函数 发请求  /patient/myUser GET 无其他参数
  // request<真正数据的类型>()
  const res = await request<{
    id: string
    avatar: string
  }>('/patient/myUser', 'GET')
  console.log(res.data.avatar)

  // const res = await request<>('/patient/myUser', 'GET')
  // console.log(res)
}
</script>

<template>
  {{ store.user }}

  <van-button type="primary" @click="login">登录</van-button>
  <van-button type="primary" @click="fn">获取个人用户信息</van-button>
  <van-button type="success" @click="store.delUser">退出</van-button>
</template>

<style scoped lang="scss"></style>
